//index.js
const formatTime = require('./../../utils/util').formatTime;
const $api = require('./../../utils/request').API
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dateString: "",
    spot: [],
    defaultTime:'',
    originData: [],
    currentData: {
      shiftName: '',
      types: ''
    },

    typeList: [
      {value: '0', label: '旷工'},
      {value: '1', label: '正常'},
      {value: '2', label: '事假'},
      {value: '3', label: '迟到'},
      {value: '4', label: '早退'},
      {value: '5', label: '病假'},
      {value: '6', label: '法定节假日'},
      {value: '7', label: '离职'},
    ]
  },


  dateChange(e) {
    const preDate = this.data.dateString.substring(0, 7)
    this.setData({
      dateString: e.detail.dateString
    })
    if( preDate === e.detail.dateString.substring(0, 7) ) {
      this.getDataByDate()
    } else {
      this.getData()
    }
   },

  getData() {
    const params = {
      date: this.data.dateString ? this.data.dateString.substring(0, 7) : this.getNowFormatDay().substring(0, 7),
    }
    $api.getKaoQing(params).then( res => {
      const arr = res.data[0]? res.data[0].dateAndShiftList : []
      this.setData({
        originData: arr,
        spot: this.getSpotData(arr)
      })
      this.getDataByDate()
    })
  },

  getSpotData(arr) {
    if (!arr && arr.length == 0) {
      return []
    }
    const res = arr.map( i => {
      return {date: i.date, name: i.types? this.data.typeList.find( j => j.value == i.types).label : ''}
    })
    return res
  },

  getDataByDate() {
    const date = this.data.dateString
    if (this.data.originData.find(i => i.date == date)) {
      const res = this.data.originData.find(i => i.date == date)
      this.setData({
        currentData: {
          shiftName: res.shiftName ? res.shiftName: '',
          types: res.types? this.data.typeList.find( i => i.value == res.types).label : ''
        }
      })
    } else {
      this.setData({
        currentData: {
          shiftName: '',
          types: ''
        }
      })
    }
  },


getNowFormatDay() {
    //显示当前时间
    function completeDate(value) 
    {
      return value < 10 ? '0' + value : value
    }
    var char = '-'
    var nowDate = new Date()
    var day = nowDate.getDate()
    var month = nowDate.getMonth() + 1 //注意月份需要+1
    var year = nowDate.getFullYear()
    //补全0，并拼接
    return year + char + completeDate(month) + char + completeDate(day)
},


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
